package com.example.canvasdemo_02;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.example.canvasdemo_02.MyThread.NetLoginThread;
import com.example.canvasdemo_02.databinding.FragmentFirstBinding;

import org.json.JSONException;
import org.json.JSONObject;

public class FirstFragment extends Fragment {

    static String CurrentAccount;
    private FragmentFirstBinding binding;
    //private mySQLite myDatabaseHelper;
    String  result="-1";

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentFirstBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        binding.buttonFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(binding.editAccount.getText().toString().isEmpty()||binding.editPassword.getText().toString().isEmpty()){
                    Toast.makeText(getContext(),"请输入账户和密码",Toast.LENGTH_LONG).show();
                }else {
                    JSONObject params = new JSONObject();
                    try {
                        params.put("account", binding.editAccount.getText().toString());
                        params.put("password", binding.editPassword.getText().toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    NetLoginThread loginThread = new NetLoginThread(params);
                    loginThread.start();
                    try {
                        loginThread.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    result=loginThread.getResult();
                    if(result.equals("-2")){
                        Toast.makeText(getContext(),"网络连接失败",Toast.LENGTH_LONG).show();
                    }else if(!result.equals("-1")||(binding.editAccount.getText().toString().equals("admin"))){
                        //登录成功，进行页面转换
                        NavHostFragment.findNavController(FirstFragment.this)
                                .navigate(R.id.action_FirstFragment_to_SecondFragment);
                        CurrentAccount=binding.editAccount.getText().toString();
                    }else{
                        Toast.makeText(getContext(),"账户或者密码输入错误",Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
        binding.buttonRegister.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //Log.i("Tag","hello");
                //myDatabaseHelper.getWritableDatabase();
                NavHostFragment.findNavController(FirstFragment.this)
                        .navigate(R.id.action_FirstFragment_to_Register);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}